<?php

namespace App\Http\Controllers\Gas;

use App\Http\Controllers\Controller;
use App\Imports\Gas\GasImport;
use App\Models\Gas\GasYh;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Facades\Excel;

class GasYhController extends Controller
{
    public function gasYhList(Request $request)
    {
        $params = $countParams = $request->all();
        unset($countParams['pageNumber'], $countParams['pageSize']);
        $data = $this->getGasYhList($params)->get()->toArray();
        $dataCount = $this->getGasYhList($countParams)->count();

        return response()->formatJson(['gasYhRecord' => $data, 'count' => $dataCount]);
    }

    public function getGasYhList($params): \Illuminate\Database\Eloquent\Builder
    {
        $query = GasYh::query();

        if (isset($params['license_plate_num'])) {
            $query->where('license_plate_num', 'like', '%'.$params['license_plate_num'].'%');
        }

        if (isset($params['line']) && '-1' !== $params['line']) {
            $query->where('line', $params['line']);
        }

        if ($params['date_time'][0] !== null && $params['date_time'][1] !== null) {
            $startDate = date('Y-m-d 00:00:00', strtotime($params['date_time'][0]));
            $endDate = date('Y-m-d 23:59:59', strtotime($params['date_time'][1]));
            $query->whereBetween('date_time', [$startDate, $endDate]);
        }

        if (isset($params['pageNumber'], $params['pageSize'])) {
            $query
                ->limit($params['pageSize'])
                ->offset(($params['pageNumber'] - 1) * $params['pageSize']);
        }

        return $query->orderByDesc('date_time');
    }

    /**
     * 导入加气记录
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function importGasYh(Request $request)
    {
        $urlParse = parse_url($request->input('url'));
        $excelData = Excel::toArray(new GasImport, public_path().$urlParse['path']);
        $fullData = $excelData[0];

        // 删除前三行
        array_shift($fullData);
        array_shift($fullData);
        array_shift($fullData);

        $data = [];
        $fields = [
            'date_time',
            'custom_type',
            'card_type',
            'card_num',
            'license_plate_num',
            'consumption_type',
            'gas_volume',
            'gas_amt',
            'preferential_settlement',
            'give_away_amt',
            'preferential_amt',
            'card_balance',
            'operator',
            'deduction_time',
            'status',
        ];
        // 整理数据，填充合并的单元格数据
        foreach ($fullData as $row) {
            $item = [];
            foreach ($fields as $index => $field) {
                $item[$field] = $row[$index];
            }
            if ($item['license_plate_num']) {
                $item['license_plate_num'] = Str::of($item['license_plate_num'])->replace('陕G', '');
            }
//            $item['date_time'] = $this->transformDateTime($item['date_time'], 'Y-m-d H:i:s');
//            $item['deduction_time'] = $this->transformDateTime($item['deduction_time']);
            $item['created_at'] = date('Y-m-d H:i:s');
            $data[] = $item;
        }

        return response()->formatJson(GasYh::query()->insert($data), '导入成功');
    }
}
